//
//  XMTitleView.m
//  XMWeibo
//
//  Created by chi on 14-8-21.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMTitleView.h"

@implementation XMTitleView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //设置属性
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        
        [self setBackgroundImage:[UIImage stretchableImageWithName:@"navigationbar_filter_background_highlighted"] forState:UIControlStateHighlighted];
        self.adjustsImageWhenHighlighted = NO;
        
        //对齐方式
        self.imageView.contentMode = UIViewContentModeCenter;
//        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        
        
        //设置形变属性
        self.imageView.transform = CGAffineTransformMakeRotation(M_PI);
        self.isArrowUP = YES;
        
    }
    return self;
}

/**
 *  调整titleLabel位置
 */
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleX = 5.0;
    CGFloat titleY = 0.0;
    CGFloat titleW = contentRect.size.width - self.currentImage.size.width;
    CGFloat titleH = contentRect.size.height;

    return CGRectMake(titleX, titleY, titleW, titleH);
}

/**
 *  调整uiimageview位置
 */
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    
    CGFloat imageY = 0.0;
    CGFloat imageW = self.currentImage.size.width;
    CGFloat imageH = contentRect.size.height;
    CGFloat imageX = contentRect.size.width - imageW - 5.0;
    
    return CGRectMake(imageX, imageY, imageW, imageH);
}

/**
 *  重写设置标题计算frame
 */
- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
    [super setTitle:title forState:state];
    
    CGSize size = [title sizeWithFont:self.titleLabel.font];
    
    CGRect frame = self.frame;
    frame.size.width = size.width + 5.0 * 2 + self.imageView.bounds.size.width + 5.0;
    
    self.frame = frame;
    
}


@end
